package com.gykj.admin.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gykj.admin.common.constants.RespResultConstants;
import com.gykj.admin.common.entity.User;
import com.gykj.admin.common.resp.RespResultJson;

@RestController
public class LoginCtl {
	
    @RequestMapping(value="/loginCheck",method=RequestMethod.POST)
    public RespResultJson loginCheck(@RequestBody User user) {
    	RespResultJson result = new RespResultJson();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserNameEg(),user.getPwd());
        try {
            subject.login(token);
            result.setStatus(RespResultConstants.T);
        }catch (LockedAccountException lae) {
            token.clear();
            result.setStatus(RespResultConstants.F);
            result.setMessage(lae.getMessage());
        } catch (AuthenticationException e) {
            token.clear();
            result.setStatus(RespResultConstants.F);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
